package com.anche.cywebnew.Controller.securityManager;

import com.anche.cywebnew.Service.securityManager.CurrUserService;
import com.anche.cywebnew.Utils.result.ResultUtil;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;

@Controller
@RequestMapping("/security/currUser")
public class CurrUserController {

    private static final Logger logger = LoggerFactory.getLogger(CurrUserController.class);

    @Autowired
    private EnterpriseCacheSessionDAO enterpriseCacheSessionDAO;

    @Autowired
    private CacheManager shiroEhcacheManager;

    @GetMapping("/manager")
    public String manager() {
        return "securityManager/currUser";
    }

    @RequestMapping("/currUserPager")
    @ResponseBody
    public Object getCurrUserPager(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        Map<String, Object> pageMap = new HashMap<>();
        int start = limit * (page - 1);
        int end = limit * page;
        Set<Object> keys = shiroEhcacheManager.getCache("shiro-kickout-session").keys();
        List<Map<Object, Object>> maps = new ArrayList<Map<Object, Object>>();
        List<Map<Object, Object>> pagesss = new ArrayList<Map<Object, Object>>();
        for (Object object : keys) {
            Map<Object, Object> map = new HashMap<Object, Object>();
            Deque<Serializable> o = (Deque<Serializable>) shiroEhcacheManager.getCache("shiro-kickout-session").get(object);
            Serializable sessionid = o.getLast();
            map.put("sessionid", sessionid);
            map.put("name", object);
            maps.add(map);
        }

        for (int i = start; i < end; i++) {
            if (i < maps.size()) {
                pagesss.add(maps.get(i));
            } else {
                break;  //直接结束for循环
            }

        }
        pageMap.put("code", "0");
        pageMap.put("count", maps.size());
        pageMap.put("msg", "查询成功");
        pageMap.put("data", pagesss);
        return pageMap;
    }

    @DeleteMapping("/kickOut")
    @ResponseBody
    public Object kickoutCurrUser(@RequestParam("name") String name, @RequestParam("sessionid") String sessionid) {
        try {
            Collection<Session> sessions = enterpriseCacheSessionDAO.getActiveSessions();
            for (Session session : sessions) {
                Serializable sessionId = session.getId();
                if (sessionId.equals(sessionid)) {
                    session.setTimeout(0);
                    shiroEhcacheManager.getCache("shiro-kickout-session").remove(name);
                    break;
                }
            }
            logger.info("踢出用户：[{}]成功", name);
            Object str = "踢出成功";
            return ResultUtil.success(str);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("踢出用户：[{}]异常<{}>", name, e);
            return ResultUtil.error(0, e.getMessage());
        }
    }
}
